package jia.game;

import jia.core.GameStateManager;
import jia.core.Compass;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.io.IOException;
import org.apache.log4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: stelle
 * Date: Jan 24, 2006
 * Time: 11:41:18 AM
 * To change this template use File | Settings | File Templates.
 */
public class CompassPanel extends JPanel {
    //private JLabel testLabel = new JLabel();
    private static final Logger log = Logger.getLogger(CompassPanel.class);
    private BufferedImage img;
    private String northImage = "images/north.png";
    private String westImage = "images/west.png";
    private String eastImage = "images/east.png";
    private String southImage = "images/south.png";
    
    private GameStateManager mgr;
    
    public CompassPanel() {
        init();
    }
    
    
    private void init(){
        this.setSize(162, 68);
        this.setVisible(true);
        this.setBackground(ComponentUtils.getGlobalBackgroundColor());
        this.setForeground(ComponentUtils.getGlobalForegroundColor());
        this.setFocusable(false);
        this.setOpaque(false);
        //testLabel.setForeground(ComponentUtils.getGlobalForegroundColor());
        //testLabel.setVisible(true);
        //this.add(testLabel);
    }
    
    
    /**
     * @param gameState
     */
    public void registerGameState(GameStateManager gameState) {
        mgr = gameState;
    }
    
    /**
     * @param gameState
     */
    public void updateGameState(GameStateManager gameState) {
        this.mgr = gameState;
        //this.testLabel.setText(gameState.getPlayerState().getFacing().getCardinalName());
        if (gameState.getPlayerState().getFacing().getDirectionalValue() == Compass.NORTH_VAL){
            if (GenericFlags.debugMessages){
                log.debug("Facing North");
            }
            setImage(northImage);
            
        }
        if (gameState.getPlayerState().getFacing().getDirectionalValue() == Compass.SOUTH_VAL){
            if (GenericFlags.debugMessages){
                log.debug("Facing South");
            }
            setImage(southImage);
            
            
        }
        if (gameState.getPlayerState().getFacing().getDirectionalValue() == Compass.EAST_VAL){
            if (GenericFlags.debugMessages){
                log.debug("Facing East");
            }
            setImage(eastImage);
            
            
        }
        if (gameState.getPlayerState().getFacing().getDirectionalValue() == Compass.WEST_VAL){
            if (GenericFlags.debugMessages){
                log.debug("Facing West");
            }
            setImage(westImage);
            
            
        }
    }
/*
 
        public JLabel getTestLabel() {
                return testLabel;
        }
 
        public void setTestLabel(JLabel testLabel) {
                this.testLabel = testLabel;
        }
 */
    
    public void setImage(String imageFile){
        
        URL skinImageFileUrl = getClass().getClassLoader().getResource(imageFile);
        try {
            img = ImageIO.read(skinImageFileUrl);
        } catch (IOException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
            
        }
        repaint();
        
        
    }
    
    protected void paintComponent(Graphics g){
        if (img != null) {
            
            g.drawImage(img, 0, 0, null);
        }
        super.paintComponent(g);
        
    }
    
}
